package com.kong.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.burgeon.commons.unique.IDWorker;
import org.seed.mybatis.core.PageInfo;
import org.seed.mybatis.core.annotation.DbTypeEnum;
import org.seed.mybatis.core.query.Query;
import org.seed.mybatis.core.query.param.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.burgeon.commons.react.Result;
import com.kong.dao.entity.Datasource;
import com.kong.service.DatasourceService;

import java.util.Date;
import java.util.stream.Collectors;

/**
 * 数据源
 *
 * @author glin
 * @date 2024-04-16
 */
@RestController
@RequestMapping("datasource")
@Tag(name = "数据源")
public class DatasourceController {

    @Autowired
    private DatasourceService datasourceService;

    /**
     * 分页查询
     *
     * @param param 分页参数
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询")
    @Parameters({
            @Parameter(name = "pageIndex", description = "当前页码"),
            @Parameter(name = "pageSize", description = "每页记录数")
    })
    public Result<PageInfo<Datasource>> page(PageParam param) {
        Query query = param.toQuery();
        PageInfo<Datasource> pageInfo = datasourceService.page(query);
        return Result.ok(pageInfo);
    }

    /**
     * 新增记录
     *
     * @param datasource
     * @return
     */
    @PostMapping("/save")
    @Operation(summary = "新增记录")
    public Result<String> save(String url,
                               String username,
                               String password,
                               String jdbcDriverClass,
                               String dbType
    ) {
        Datasource datasource = new Datasource();
        datasource.setId(IDWorker.nextId());
        datasource.setUrl(url);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setJdbcDriverClass(jdbcDriverClass);
        if (!DbTypeEnum.getSupportDbType().contains(dbType.toLowerCase())) {
            String all = DbTypeEnum.getSupportDbType().stream().collect(Collectors.joining(","));
            return Result.errorMessageOnly("dbType仅支持如下数据类型: " + all);
        }
        datasource.setDbType(dbType);
        datasource.setUpdatedAt(new Date());
        datasourceService.saveIgnoreNull(datasource);
        // 返回添加后的主键值
        return Result.ok(datasource.getId());
    }

    /**
     * 修改记录
     *
     * @param datasource 表单数据
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "修改记录")
    public Result<Void> update(String id,
                               @RequestParam(required = false) String url,
                               @RequestParam(required = false) String username,
                               @RequestParam(required = false) String password,
                               @RequestParam(required = false) String jdbcDriverClass,
                               @RequestParam(required = false) String dbType) {
        Datasource store = datasourceService.getById(id);
        if (store == null) {
            return Result.errorMessageOnly("无数据记录");
        }
        store.setUrl(url);
        store.setUsername(username);
        store.setPassword(password);
        store.setJdbcDriverClass(jdbcDriverClass);
        if (StringUtils.hasText(dbType) && !DbTypeEnum.getSupportDbType().contains(dbType.toUpperCase())) {
            String all = DbTypeEnum.getSupportDbType().stream().collect(Collectors.joining(","));
            return Result.errorMessageOnly("dbType仅支持如下数据类型: " + all);
        }
        store.setDbType(dbType);
        store.setUpdatedAt(new Date());
        datasourceService.updateIgnoreNull(store);
        return Result.ok();
    }

    /**
     * 删除记录
     *
     * @param id 主键id
     * @return
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除记录")
    public Result<Void> delete(String id) {
        datasourceService.deleteById(id);
        return Result.ok();
    }


    /**
     * 根据ID查询
     *
     * @param id 主键id
     * @return
     */
    @GetMapping("/get")
    @Operation(summary = "根据ID查询")
    public Result<Datasource> get(String id) {
        Datasource datasource = datasourceService.getById(id);
        return Result.ok(datasource);
    }

}